﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for UserDAO
/// </summary>
public class UserDAO
{
    #region truy xuất dữ liệu
    public static bool KtratontaiTK(UserDTO usDTO)
    {
        try
        {
            string sQuery = "select * from NGUOIDUNG where ID = '" + usDTO.ID1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public static bool KtraDangNhap(UserDTO usDTO)
    {
        try
        {
            string sMKmahoa = MahoaMK(usDTO.MK1);
            string sQuery = "select * from NGUOIDUNG where ID ='" + usDTO.ID1 + "'and PASS ='" + sMKmahoa + "' and PhanQuyen='2'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool KtraDangNhapADmin(UserDTO usDTO)
    {
        try
        {
            string sMKmahoa = MahoaMK(usDTO.MK1);
            string sQuery = "select * from NGUOIDUNG where ID ='" + usDTO.ID1 + "'and PASS ='" + sMKmahoa + "' and PhanQuyen='1'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    #endregion

    #region thêm dữ liệu và mã hóa

    public static string MahoaMK(string sMK)
    {
        try
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sMK));
            byte[] EncodeMK = md5.Hash;
            string sMKmahoa = "";
            for (int i = 0; i < EncodeMK.Length; i++)
            {
                sMKmahoa += EncodeMK[i].ToString();
            }
            return sMKmahoa;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    
    public static bool ThemUserKH(UserDTO ndDTO)
    {
        try
        {
            string sMKmahoa = MahoaMK(ndDTO.MK1);
            string sQuery = "INSERT INTO NGUOIDUNG(ID, EMAIL, PASS, NGAYSINH, NGAYDK, CMND, CUAHOIBAOMAT, TRALOI, GioiTinh, TENFULL,PhanQuyen) VALUES('" + ndDTO.ID1 + "','" + ndDTO.Email1 + "','" + sMKmahoa + "','" + ndDTO.NgaySinh1 + "','" + ndDTO.NgayDangKy1 + "','" + ndDTO.CMND1 + "','" + ndDTO.CauHoi1 + "','" + ndDTO.TraLoi1 + "','" + ndDTO.GioiTinh1 + "','" + ndDTO.TENKH1 + "','2')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    #endregion

    public static bool suaMK(UserDTO ndDTO)
    {
        try
        {
            string sMKmahoa = MahoaMK(ndDTO.MK1);
            string sQuery = "update NguoiDung set Pass='"+sMKmahoa+"' where ID='"+ ndDTO.ID1+ "'" ;
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool suaThontin(UserDTO stDTO)
    {
        try
        {
            string sQuery = "update NGUOIDUNG set EMAIL='" + stDTO.Email1 + "',NGAYSINH='" + stDTO.NgaySinh1 + "', CMND='" + stDTO.CMND1 + "', CUAHOIBAOMAT='" + stDTO.CauHoi1 + "', TRALOI='" + stDTO.TraLoi1 + "', GioiTinh='" + stDTO.GioiTinh1 + "', TENFULL='" + stDTO.TENKH1 + "', PhanQuyen='" + stDTO.PhanQuyen1 + "' Where  ID='" + stDTO.ID1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

}